<?php
/**
 * Created by PhpStorm.
 * Author: Shadow
 * Date: 2022/5/31
 * Time: 9:35 上午
 * description: UserBusiness.php
 * 用户有关的业务逻辑类
 */

namespace app\home\business;


use think\facade\Cache;
use think\facade\Session;

class UserBusiness
{

    /**
     * 注册方法
     * 可以用手机号注册/也可以第三登录
     * @param $param
     * @param int $type * 默认是1 手机号注册 2 QQ注册
     * @return bool
     */
    public static function  register($param,$type=1){
//        进行入库
//        INSERT INTO `2002_pyg`.`pyg_user`(`id`, `username`, `nickname`, `phone`, `password`, `figure_url`, `email`, `last_login_time`, `open_type`, `openid`, `create_time`, `update_time`, `delete_time`) VALUES (3, '19712345678', '197****5678', '19712345678', '8ea6eaee0c5819e756b604c998639b13', NULL, '', 0, NULL, '', 1554471633, 1554471633, NULL);

        switch ($type) {
            case 2:
                // qq 登录
                # 需要判断该手机号是否注册过、不能重复注册
                $is_auth = \app\common\model\User::where('openid','=',$param['open_id'])->find();

                if ($is_auth) {
                    # 已经授权过、可以更新一下用户信息
                    $userInfo = \app\common\model\User::update(['nickname' => $param['nickname'],'figure_url' => $param['figureurl_2']],['id' => $is_auth->id]);
                    \session('userInfo',[
                        'username' => $userInfo->username,
                        'nickname' => $userInfo->nickname,
                        'figure_url' => $userInfo->figure_url,
                        'id' => $userInfo->id
                    ]);
                    return  true;
                }
                $data = [
                    'username' => $param['nickname'],
                    'figure_url' => $param['figureurl_2'],
                    'nickname' => $param['nickname'],
                    'password' => md5('2002a1234562202a'),
                    'open_type' => 'qq',
                    'openid' => $param['open_id'],
                    'create_time' => time(),
                    'last_login_time' => time(),
                ];

                break;
            default:
                # 需要判断该手机号是否注册过、不能重复注册
                $is_register = \app\common\model\User::where('phone','=',$param['phone'])->find();
                if ($is_register) {
                    abort(2001,'该手机已经注册过、请直接登录');
                }
                // 默认手机号注册
                $data = [
                    'username' => $param['phone'],
                    'nickname' => encrypt_phone($param['phone']),
                    'phone' => $param['phone'],
                    'password' => md5('2002a'.$param['password'].'2202a'),// 密码需要加密、封装一个密码加密的方法
                ];
        }

//        调用添加方法入库
        $result = \app\common\model\User::create($data);
        if ($type!=1) {
            \session('userInfo',[
                'nickname' => $result->nickname,
                'username' => $result->username,
                'figure_url' => $result->figure_url,
                'id' => $result->id
            ]);
        }
        if (!$result) {
            abort(2003,"注册失败");
        }

        return true;


    }

    /**
     * @param $phone *手机号
     * @param $code * 用户输入的验证码
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function checkCode($phone,$code){
        $redis_code = Cache::store('redis')->get('code_'.$phone);
        if ($redis_code != $code) {
            abort(2001,"验证码错误");
        }

        # 验证验证码通过以后 、销毁验证码
        Cache::store('redis')->rm('code_'.$phone);
        return true;
    }

    /**
     * 进行登录
     * 邮箱、手机号也可以登录、用户名
     */
    public static function doLogin($param){
//        $userInfo = \app\common\model\User::whereOr()->get();

        if (strpos($param['account'],'@')) {// 可以写正则表达式
            $userInfo = \app\common\model\User::where('email','=',$param['account'])->find();
        } elseif (strlen($param['account']) == 11) {
            $userInfo = \app\common\model\User::where('phone','=',$param['account'])->find();
        } else {
            $userInfo = \app\common\model\User::where('username','=',$param['account'])->find();
        }

        if (!$userInfo) {
            abort(2001,"用户名不存在");
        }
        if ($userInfo->password!= md5('2002a'.$param['password'].'2202a')) {
            abort(2002,'密码错误');
        }
        # 需要存储用户信息
        Session::set('userInfo',$userInfo);
        return true;
    }

    /**
     * 获取登录的用户id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Session\SessionManager|\Illuminate\Session\Store|int|mixed
     */
    public static function getUserId(){
        return \session('userInfo.id') ?: 0;
    }

}